/*
*
*  签到数据模型
*  
*
*/
// 导入处理数据库查询的文件
const db = require('../../config/db.js')
module.exports = {
    // 编写用户签到方法
    // 参数：signin_id, user_id, signin_create_time, signin_status, signin_year, signin_month, signin_date,user_name
    add(signin_id, user_id, signin_create_time, signin_status, signin_year, signin_month, signin_date,user_name) {
        return new Promise((resolve, reject) => {
            db.query("select * from db_signin2 where user_id='" + user_id + "' and signin_year = '" + signin_year + "' and signin_month = '" + signin_month + "' and signin_date = '" + signin_date + "'", (err, rows) => {
                if (err) {
                    resolve({ code: -1, msg: '签到查询失败。' })
                } else {
                    if (rows.length == 0) {
                        db.query("insert into db_signin2(signin_id,user_id,signin_create_time,signin_status,signin_year,signin_month,signin_date,user_name) values('" + signin_id + "','" + user_id + "','" + signin_create_time + "','" + signin_status + "','" + signin_year + "','" + signin_month + "','" + signin_date + "','"+user_name+"')", (err, rows) => {
                            if (err) {
                                resolve({ code: -1, msg: '签到失败。' })
                            } else {
                                resolve({ code: 200, msg: '签到成功。', signin_status: signin_status })
                            }
                        })
                    } else {
                        resolve({ code: 201, msg: '已签到。', signin_status: rows['signin_status'] })
                    }
                }
            })
        })
    },
    // 编写查询用户签到方法
    // 参数：signin_id,user_id,signin_create_time,signin_status
    list(user_id, signin_year, signin_month, signin_date) {
        return new Promise((resolve, reject) => {
            db.query("select * from db_signin2 where user_id = '" + user_id + "' and signin_year = '" + signin_year + "' and signin_month = '" + signin_month + "' and signin_date = '" + signin_date + "'", (err, rows) => {
                if (err) {
                    resolve({ code: -1, msg: '签到查询失败。' })
                } else {
                    if (rows.length == 0) {
                        resolve({ code: 200, msg: '未签到。', signin_status: rows['signin_status'] })
                    } else {
                        resolve({ code: 200, msg: '已签到。', signin_status: rows['signin_status'] })
                    }
                }
            })
        })
    },

    // 编写删除签到方法
    // 参数：signin_id
    remove(signin_id) {
        return new Promise((resolve, reject) => {
            db.query("select * from db_signin2 where signin_id='" + signin_id + "'", (err, rows) => {
                if (err) {
                    resolve({ code: -1, msg: '查询签到失败。' })
                } else {
                    db.query("delete from  db_signin2 where signin_id = '" + signin_id + "'", function (err, rows) {
                        if (err) {
                            resolve({ code: -1, msg: '删除签到失败。' })
                        } else {
                            resolve({ code: 200, msg: '删除签到成功。' })
                        }
                    })
                }
            })
        })
    },

    // 编写统计用户签到次数方法
    // 参数：user_id
    total(user_id) {
        return new Promise((resolve, reject) => {
            db.query("select * from db_signin2 where user_id='" + user_id + "'", (err, rows) => {
                if (err) {
                    resolve({ code: -1, msg: '签到查询次数失败。' })
                } else {
                    if (rows.length == 0) {
                        resolve({ code: 200, msg: '查询签到次数。', total: 0 })
                    } else {
                        let count = 0;
                        for (let i = 0; i < rows.length; i++) { count++; }
                        resolve({ code: 200, msg: '查询签到次数。', total: count })
                    }
                }
            })
        })
    },
    // 统计今天签到人数
    // 参数：年份 月份 日期
    // 编写统计用户人数方法
    // all(signin_year, signin_month, signin_date) {
    //     return new Promise((resolve, reject) => {
    //         // let sql = "select count(*) as total from db_signin2 where signin_year = '" + signin_year + "' and signin_month = '" + signin_month + "' and signin_date = '" + signin_date + "'";
    //         let sql = "select * from db_signin2 where signin_year = '" + signin_year + "' and signin_month = '" + signin_month + "' and signin_date = '" + signin_date + "'";
    //         // console.log(sql)
    //         db.query(sql, function (err, rows) {
    //             if (err) {
    //                 resolve({
    //                     code: -1,
    //                     msg: '统计今天签到人数失败。',
    //                     err
    //                 })
    //             } else {
    //                 resolve({
    //                     code: 200,
    //                     msg: "获取今天签到人数。",
    //                     result: rows,
    //                     // total: rows[0]['total']
    //                 })
    //             }
    //         })
    //     })
    // },
    // 编写签到分页列表方法
    // 参数：page_num,page_size
    items(page_num, page_size) {
        return new Promise((resolve, reject) => {
            const params = [(parseInt(page_num) - 1) * parseInt(page_size), parseInt(page_size)]
            db.query("select * from db_signin2 limit " + params[0] + "," + params[1] + "", function (err, rows) {
                if (err) {
                    resolve({
                        code: -1,
                        msg: '查询签到失败。',
                        err
                    })
                } else {
                    //as更换名称
                    db.query('select count(*) as total from db_signin2', function (error, among) {
                        if (error) {
                            resolve({
                                code: -1,
                                msg: '统计签到数量失败。',
                                error
                            })
                        } else {
                            resolve({
                                code: 200,
                                msg: "获取签到列表成功。",
                                result: rows,
                                page: {
                                    page_num: page_num,
                                    page_size: page_size,
                                    total: among[0]['total'] //查询表中的数量
                                }
                            })
                        }
                    })
                }
            })

        })
    },
    // 编写统计签到方法
    test(signin_year, signin_month, signin_date) {
        // console.log({signin_year, signin_month, signin_date})
        return new Promise((resolve, reject) => {
            let sql = "select count(*) as total from db_signin2 where signin_year = '" + signin_year + "' and signin_month = '" + signin_month + "' and signin_date = '" + signin_date + "'";
            // console.log(sql);
            db.query(sql, function (error, among) {
                if (error) {
                    resolve({
                        code: -1,
                        msg: '统计签到数量失败。',
                        error
                    })
                } else {
                    resolve({
                        code: 200,
                        msg: "获取今天签到总人数成功。",
                        total: among[0]['total']
                    })
                }
            })
        })
    },
    // 是否签到
    isSignin(user_id, signin_year, signin_month, signin_date) {
        return new Promise((resolve, reject) => {
            db.query("select * from db_signin2 where user_id = '" + user_id + "' and signin_year = '" + signin_year + "' and signin_month = '" + signin_month + "' and signin_date = '" + signin_date + "'", (err, rows) => {
                if (err) {
                    resolve({ code: -1, msg: '签到查询失败。' })
                } else {
                    if (rows.length == 0) {
                        resolve({ code: -1, msg: '未签到。', signin_status: rows['signin_status'] })
                    } else {
                        resolve({ code: 200, msg: '已签到。', signin_status: rows['signin_status'] })
                    }
                }
            })
        })
    },
}


